<?php

namespace backend\modules\marketing\models;

use frontend\modules\marketing\models\CouponOrder;
use frontend\modules\marketing\models\CouponUser;
use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "coupon".
 *
 * @property string $coupon_id
 * @property string $coupon_name
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $coupon_number
 * @property string $coupon_money
 * @property string $coupon_mustmoney
 */
class Coupon extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'coupon';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['coupon_id','default','value'=>$this->getUUID()],
            [['coupon_id','start_time','end_time', 'coupon_number','coupon_money', 'coupon_mustmoney','coupon_name'], 'required'],
            [['start_time', 'end_time', 'coupon_number'], 'integer'],
            [['coupon_money', 'coupon_mustmoney'], 'number'],
            [['coupon_id'], 'string', 'max' => 40],
            [['coupon_name'], 'string', 'max' => 60],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'coupon_id' => Yii::t('app', 'Coupon ID'),
            'coupon_name' => Yii::t('app', '优惠券名称'),
            'start_time' => Yii::t('app', '开始时间'),
            'end_time' => Yii::t('app', '结束时间'),
            'coupon_number' => Yii::t('app', '优惠券数量'),
            'coupon_money' => Yii::t('app', '减价'),
            'coupon_mustmoney' => Yii::t('app', '满价'),
        ];
    }

    /**
     * @inheritdoc
     * @return \backend\modules\marketing\models\query\CouponQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \backend\modules\marketing\models\query\CouponQuery(get_called_class());
    }

    public function afterFind()
    {
        $this->end_time = date('Y-m-d',$this->end_time);
        $this->start_time = date('Y-m-d',$this->start_time);
    }
}
